﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class backmanage_API_A_Admanage : System.Web.UI.Page
{
    /// <summary>
    /// ADSYSTEM AD.API 广告系统API
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Expires = -1;
        string returnStr = string.Empty;
        string editType = Request.Params["edit"];//执行的操作
        switch (editType)
        {
            case "add":
                {
                    returnStr = AdAdd();
                    break;
                }
            case "del":
                {
                    returnStr = AdDel();
                    break;
                }
            case "update":
                {
                    returnStr = AdUpdate();
                    break;
                }
            case "list":
                {
                    returnStr = AdList();
                    break;
                }
            case "positionlist":
                {
                    returnStr = AdPositionList();
                    break;
                }
            case "state":
                {
                    returnStr = AdpdateState();
                    break;
                }
            default: break;
        }
        Response.Write(returnStr);
    }
    /// <summary>
    /// 增加广告
    /// </summary>
    /// <returns></returns>
    protected string AdAdd()
    {
        MODEL.AdInformation model = new MODEL.AdInformation();
        BLL.AdInformation bll = new BLL.AdInformation();
        model.AID = bll.GetMaxId();
        model.AbeginTime = Convert.ToDateTime(Request.Params["bt"]);
        model.AendTime = Convert.ToDateTime(Request.Params["et"]);
        model.Aimgurl = Request.Params["iu"].ToString();
        model.Aposition = Request.Params["p"].ToString();
        model.Astate = Convert.ToBoolean(Convert.ToInt32(Request.Params["s"]));
        model.Atitle = Request.Params["t"].ToString();
        model.Aurl = Request.Params["u"].ToString();
        return bll.InformationAdd(model).ToString();
    }
    /// <summary>
    ///  删除广告
    /// </summary>
    /// <returns></returns>
    protected string AdDel()
    {
        string[] idList = Request.Params["id[]"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
        string errId = string.Empty;
        for (int i = 0; i < idList.Length; i++)
        {
            BLL.AdInformation bll = new BLL.AdInformation();
            bool flag = bll.InformationDel(idList[i]);
            if (flag)
            {
                errId = "True";
            }
            else
            {
                errId += idList[i] + ",";
            }
        }
        return errId;
    }
    /// <summary>
    /// 修改广告内容
    /// </summary>
    /// <returns></returns>
    protected string AdUpdate()
    {
        MODEL.AdInformation model = new MODEL.AdInformation();
        BLL.AdInformation bll = new BLL.AdInformation();
        model.AID = Request.Params["id"].ToString();
        model.AendTime = Convert.ToDateTime(Request.Params["et"]);
        model.Aimgurl = Request.Params["iu"].ToString();
        model.Aposition = Request.Params["p"].ToString();
        model.Astate = Convert.ToBoolean(Convert.ToInt32(Request.Params["s"]));
        model.Atitle = Request.Params["t"].ToString();
        model.Aurl = Request.Params["u"].ToString();
        return bll.InformationUpdate(model).ToString();
    }
    /// <summary>
    /// 广告列表
    /// </summary>
    /// <returns></returns>
    protected string AdList()
    {
        BLL.AdInformation bll = new BLL.AdInformation();
        DataSet ds = bll.GetAdInformationList();
        StringBuilder htmlStr=new StringBuilder();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            htmlStr.Append("<tr id=\"" + ds.Tables[0].Rows[i]["Aid"].ToString() + "\" title=\"" + ds.Tables[0].Rows[i]["Aimgurl"].ToString() + "\">");
            htmlStr.Append("<td><input type=\"checkbox\" value=\"" + ds.Tables[0].Rows[i]["Aid"].ToString() + "\" /></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"title\" title=\"" + ds.Tables[0].Rows[i]["Aurl"].ToString() + "\">" + ds.Tables[0].Rows[i]["Atitle"].ToString() + "</a></td>");
            htmlStr.Append("<td><span class=\"position\" title=\"" + ds.Tables[0].Rows[i]["Aposition"].ToString() + "\">" + GetPositionTitle(ds.Tables[0].Rows[i]["Aposition"].ToString()) + "</span></td>");
            htmlStr.Append("<td>" + Convert.ToDateTime(ds.Tables[0].Rows[i]["Abegintime"]).ToShortDateString() + "</td>");
            htmlStr.Append("<td><span class=\"endtime\">" + Convert.ToDateTime(ds.Tables[0].Rows[i]["Aendtime"]).ToShortDateString() + "</span></td>");
            htmlStr.Append("<td>0</td>");
            htmlStr.Append("<td>0</td>");
            htmlStr.Append("<td><a href=\"#\" class=\"state\">" + Convert.ToBoolean(ds.Tables[0].Rows[i]["Astate"]).ToString().ToLower() + "</a></td>");
            htmlStr.Append("<td><a href=\"#\" class=\"edit\">编辑</a></td>");
            htmlStr.Append("</tr>");
        }
        return htmlStr.ToString();
    }
    /// <summary>
    /// 广告位列表
    /// </summary>
    /// <returns></returns>
    protected string AdPositionList()
    {
        BLL.AdInformation bll = new BLL.AdInformation();
        StringBuilder htmlStr = new StringBuilder();
        DataSet ds = bll.GetAdpositionList();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            htmlStr.Append("<option value=\"" + ds.Tables[0].Rows[i]["Aid"].ToString() + "\">" + ds.Tables[0].Rows[i]["Atitle"].ToString() + "</option>");
        }
        return htmlStr.ToString();
    }
    /// <summary>
    /// 获取广告为标题
    /// </summary>
    /// <param name="positionId"></param>
    /// <returns></returns>
    protected string GetPositionTitle(string positionId)
    {
        BLL.AdPosition bll = new BLL.AdPosition();
        MODEL.AdPosition model = new MODEL.AdPosition();
        model = bll.GetAdPositionModel(positionId);
        return model.Atitle;
    }
    /// <summary>
    /// 修改广告可用状态
    /// </summary>
    /// <returns></returns>
    protected string AdpdateState()
    {
        MODEL.AdInformation model = new MODEL.AdInformation();
        BLL.AdInformation bll = new BLL.AdInformation();
        string strBool = Request.Params["s"].ToLower();
        bool flag = false;
        if (strBool == "true")
        {
            flag = false;
        }
        else
        {
            flag = true;
        }
        model.AID = Request.Params["id"].ToString();
        model.Astate = flag;
        return bll.InformationUpdateState(model).ToString();
    }
}
